/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

$(document).ready(function () {


    function checkDelay() {
        var delayPages = cardConfig.delayPages
        var delay = 200
        if (delayPages[moduleId]) {
            if (delayPages[moduleId][controllerId]) {
                if (delayPages[moduleId][controllerId][actionId]) {
                    return delay = 1000
                }
            }
        }
        return delay
    }

    var ajax_request = null;
    var _popoper_t;
    $(document).on('mouseenter  mouseleave', '.item-detail', function (e) {
        if (e.type === 'mouseenter') {
            cardConfig.isInItemDetail = true
        } else {
            cardConfig.isInItemDetail = false
            $('[data-item-detail]').popover('hide');
        }
    })
    $(document).on('mouseenter  mouseleave', '[data-item-detail]', function (event) {

        var el = $(this);

        if (event.type === 'mouseenter') {
            clearTimeout(_popoper_t);

            _popoper_t = setTimeout(function () {
                el.popover({
                    trigger: 'manual',
                    html: true,
                    content: function () {

                        if (typeof (el.data('content')) === 'undefined') {

                            el.data('content', '<span>loading...</span>');

                            ajax_request = $.get(el.data('item-detail'), function (response) {

                                el.data('content', response ? response : '<span class="ft-12 name-ellipsis-120">名片异常，请重新尝试</span>');

                                el.next().find("div.popover-content").html(response);

                                if (el.is(':hidden') === false) {

                                    el.popover('show');
                                }

                                if (response) {
                                    ajax_request = null;
                                }

                            });
                        }

                        return el.data('content');
                    }
                }).popover('show').on('shown.bs.popover', function () {
                    $('.popover-content').css('padding', '0')
                });
            }, checkDelay());


        } else {

            if (ajax_request !== null) {

                ajax_request.abort();

                if (el.data('content') === '<span>loading...</span>' || el.data('content') === '<span class="ft-12 name-ellipsis-120">名片异常，请重新尝试</span>') {

                    el.removeData('content');
                    el.next().find("div.popover-content").html('');
                }

            }

            clearTimeout(_popoper_t);

            setTimeout(function () {
                if (!cardConfig.isInItemDetail) {
                    el.popover('hide');
                }
            }, 300)

        }

    });

});
